package com.jxc.jxcmanage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxc.jxcmanage.code.entity.UserInfo;
import com.jxc.jxcmanage.code.mapper.UserInfoMapper;
import com.jxc.jxcmanage.constants.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	public Map<String, Object> queryUserList(Map<String, Object> param) {
		PageHelper.startPage((Integer) param.get("pageNumber"), (Integer) param.get("pageSize"));
		PageInfo<UserInfo> page = new PageInfo<>(userInfoMapper.queryUserList(param));

		Map<String, Object> resultData = new HashMap<>();
		resultData.put("total", page.getTotal());
		resultData.put("rows", page.getList());
		return resultData;
	}

	public int deleteUserInfoById(Integer id) {
		return userInfoMapper.deleteUserInfoById(id);
	}

	public int addOrUpdateUserInfo(UserInfo userInfo) {
		// 更新
		userInfo.setUpdatedBy(Constant.DEFAULT_BY);
		if (userInfo.getId() != null && userInfo.getId().intValue() > 0) {
			return userInfoMapper.updateUserInfoById(userInfo);
		} else {// 新增
			userInfo.setCreatedBy(Constant.DEFAULT_BY);
			userInfo.setUserCode(userInfo.getUserCode().toUpperCase());
			return userInfoMapper.insertUserInfo(userInfo);
		}
	}

	public UserInfo findByCode(UserInfo userInfo) {
		return userInfoMapper.findByCode(userInfo);
	}
}
